clear all
set more off
tempfile ubigeo temp

/* 

This dofile compiles all data files into the one with 94,786 observations used in the analysis.

It efines exposure variables by age groups: preschool, primary, secondary and tertiary
The overall measure if named "todo"

The addition wrt to mergedp.do is the inclusion of exposure to violence from rondas campesinas.
This means attaching two extra types of exposure: 
	1. attachviolenceATR.dta which combines government + terrorist + rondas campesinas
	2. attachviolenceR.dta which has violence from rondas campesinas only

*/

** PATHS
local working ../working

** DATA
use `working'/300, clear
merge 1:1 conglome vivienda hogar codperso year using `working'/200
keep if _merge == 3
drop _merge

merge 1:1 conglome vivienda hogar codperso year using `working'/getsample
keep if _merge == 3
drop _merge

merge 1:1 conglome vivienda hogar codperso year using `working'/trust
drop if _merge == 2
drop _merge

merge 1:1 conglome vivienda hogar codperso year using `working'/identity
drop if _merge == 2
drop _merge

merge 1:1 conglome vivienda hogar codperso year using `working'/participation
drop if _merge == 2
drop _merge

merge 1:1 conglome vivienda hogar codperso year using `working'/democracy
drop if _merge == 2
drop _merge

merge 1:1 conglome vivienda hogar codperso year using `working'/voting
drop if _merge == 2
drop _merge

merge 1:1 conglome vivienda hogar codperso year using `working'/attachviolenceAT
drop if _merge == 2
drop _merge

merge 1:1 conglome vivienda hogar codperso year using `working'/attachviolenceATR
drop if _merge == 2
drop _merge

merge 1:1 conglome vivienda hogar codperso year using `working'/attachviolenceA
drop if _merge == 2
drop _merge

merge 1:1 conglome vivienda hogar codperso year using `working'/attachviolenceT
drop if _merge == 2
drop _merge

merge 1:1 conglome vivienda hogar codperso year using `working'/attachviolenceR
drop if _merge == 2
drop _merge


recode events* (1/999 = 1)

gen prov1993 = int(ubigeo1993/100)
egen provid = group(prov1993)
*tab provid, gen(provid_)

egen trend = group(year)
replace trend = trend

recode part* (. = 0)

sum provid
local max = r(max)

forvalues p = 1/`max' {
	*replace provid_`p' = provid_`p' * trend
}	

foreach t in AT ATR A T R { 
	egen preesc`t' = rowtotal(events`t'1 events`t'2 events`t'3 events`t'4 events`t'5 events`t'6)
	egen primar`t' = rowtotal(events`t'7 events`t'8 events`t'9 events`t'10 events`t'11)
	egen second`t' = rowtotal(events`t'12 events`t'13 events`t'14 events`t'15 events`t'16 events`t'17 events`t'18)
	egen tertia`t' = rowtotal(events`t'19 events`t'20 events`t'21 events`t'22 events`t'23 events`t'24 events`t'25 events`t'26 events`t'27 events`t'28 events`t'29 events`t'30)

	gen todo`t' = preesc`t' + primar`t' + second`t' + tertia`t'
}

egen nomiss = rownonmiss(trust_*)
drop if nomiss == 0
drop nomiss

drop event*

** SAVE
compress
save `working'/mergedpR, replace
